/**
 * 
 */
package com.pactera.android.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pactera.android.R;
import com.pactera.android.R.id;
import com.pactera.android.R.layout;
import com.pactera.android.version.CheckVersionTask;
import com.pactera.android.version.UpdateManager;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author David Zheng
 *
 */
public class MoreActivity extends BaseActivity {
	
	TextView headerText;
	
	LinearLayout header;
	
	Button settingsBtn;
			
	private String[] versionNames= new String[]{"Feedback", "Check for Updates", "About"};
	
	private int[] versionImageIds = new int[]{R.drawable.ic_menu_favorite, R.drawable.ic_menu_refresh, R.drawable.ic_menu_info_details};

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_activity);
		initHeader();
		initVersionBlock();			

	}	
	
	private void initHeader() {			
		headerText = (TextView)findViewById(R.id.topTextId);
		headerText.setText(R.string.more);
		settingsBtn = (Button)findViewById(R.id.settingBtnId);
		settingsBtn.setVisibility(0);	
	}
	
	private void initVersionBlock() {
		List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
		for (int i=0; i <versionNames.length; i++) {
			Map<String, Object> item = new HashMap<String, Object>();
			item.put("versionImg", versionImageIds[i]);
			item.put("versionName", versionNames[i]);
			listItems.add(item);
		}
		
		SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.version_block, 
				new String[]{"versionName","versionImg"}, new int[]{R.id.versionTextId, R.id.versionImageId});
		
		ListView listView =  (ListView)findViewById(R.id.versionListId);
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				switch(position) {
					case 0:
						Intent feedbackIntent = new Intent(MoreActivity.this, FeedbackActivity.class); 
						MoreActivity.this.startActivity(feedbackIntent);
						break;
					case 1:
						checkVersion();
						break;
					case 2:
						Intent aboutIntent = new Intent(MoreActivity.this, AboutActivity.class); 
						MoreActivity.this.startActivity(aboutIntent);
						break;
				}
				
			}
			
		});
	}
	
	/**
	 * init a new thread to do the version check job
	 */
	private void checkVersion() {
		CheckVersionTask.checkVersion(this);
		Toast.makeText(this, "Checking For New Version, Please Wait...", Toast.LENGTH_SHORT).show();		
	}
	
	
	
	@SuppressWarnings("deprecation")
	private void sendNotification() {		
		Intent intent = new Intent(this, AboutActivity.class);
		PendingIntent pi = PendingIntent.getActivity(MoreActivity.this, 0, intent, 0);
				
		Notification notify = new Notification();
		notify.icon = R.drawable.ic_menu_info_details;
		notify.tickerText = "tickerText";
		notify.when = System.currentTimeMillis();
		notify.defaults = Notification.DEFAULT_ALL;
		notify.setLatestEventInfo(MoreActivity.this, "A New Notice", "Click to View", pi);
		
		NotificationManager notifcationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
		notifcationManager.notify(0x1234, notify);
	}
	
}
